package com.showmaker.config;

import com.showmaker.job.ClearPicJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {
    // 从配置文件中获取 cron 表达式
    @Value("${quartz.cron}")
    private String cron;

    // 创建定时任务
    @Bean
    public JobDetail quartzDetail() {
        System.out.println(cron);

        return JobBuilder.newJob(ClearPicJob.class)
                .storeDurably()
                .build();
    }

    // 创建触发器

    public Trigger trigger() {
        System.out.println(cron);

        // 每 10 秒执行一次
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);

        return TriggerBuilder.newTrigger()
                .forJob(quartzDetail())
                .withSchedule(cronScheduleBuilder)
                .build();
    }
}
